package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Admin;

import com.example.entity.User;
import com.example.service.AdminService;
import org.apache.ibatis.annotations.Mapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("admins")
public class AdminController {

    private R r;


    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @return
     */
    @PostMapping("/loginAdmin")
    public R login(HttpServletRequest request, @RequestBody Admin admin){

        LambdaQueryWrapper<Admin> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(admin.getJobNumber()),Admin::getJobNumber,admin.getJobNumber());

        Admin admin1 = adminService.getOne(lqw);

        if (admin1 != null) {
            if (admin1.getPassword()!=admin.getPassword()){
                return R.error("密码错误！");
            }else {
                request.getSession().setAttribute("user",admin1);
                return R.success("登录成功！",admin);
            }
        }else {
            return R.error("登录失败！");
        }
    }

    /**
     * 分页查询所有管理员信息
     * @return
     */

    @GetMapping("/getPageAdmin/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Admin admin){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Admin> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(admin.getJobNumber()),Admin::getJobNumber,admin.getJobNumber());

        IPage page1 = adminService.page(page,lqw);


        return R.success("分页查询所有admins成功！",page1);
    }



//    /**
//     * 查询当前管理员
//     * @return
//     */
//
//    @GetMapping("/getMyself/{id}")
//
//    public R getOne(@PathVariable int id){
//
//        Admin admin= adminService.getOne(id);
//
//        return R.success("分页查询所有admin成功！",admin);
//    }
//
//
    /**
     * 添加
     * @param admin
     * @return
     */
    @PostMapping("/addAdmin")
    public R add(@RequestBody Admin admin){

        LambdaQueryWrapper<Admin> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(admin.getJobNumber()),Admin::getJobNumber,admin.getJobNumber());
        Admin admin1=adminService.getOne(lqw);

        if (admin1==null) {
            Boolean update = adminService.save(admin);
            if (update) {
                return R.success("添加admin成功!", null);
            }
        }
        return R.error("工号已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAdmin/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = adminService.removeById(id);
        if (byId){
            return R.success("删除admins成功!",null);
        }
        return R.error("删除admins失败!");
    }

    /**
     * 修改
     * @param admin
     * @return
     */
    @PutMapping("/updateAdmin")
    public R deleteById(@RequestBody Admin admin){

        Boolean update = adminService.updateById(admin);
        if (update){
            return R.success("修改admin成功!",admin);
        }
        return R.error("修改admin失败!");
    }


}

